package com.ocom.accesservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.accesservice.dao.IDaoZoneDate;
import com.ocom.accesservice.service.ZoneDateService;
import com.ocom.common.entity.access.EntityMjZoneDate;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.web.GetZoneDateListRequest;
import com.ocom.common.utils.DateUtils;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Service
public class ZoneDateServiceImpl extends ServiceImpl<IDaoZoneDate, EntityMjZoneDate> implements ZoneDateService {


    @Autowired
    private IDaoZoneDate daoZoneDate;




    /**
     * 初始化单位门禁参数
     * */
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Transactional
    @Override
    public void InitCompanyPara(EntityMjZoneDate entityMjZoneDate,Long companyId) {
        daoZoneDate.insert(entityMjZoneDate);
    }

    /*
     * @Description: 获取时区设置列表,按page=0不分页
     * @Author: lu 
     * @Date: 2022/1/13 15:26
     * @param comId:  
     * @return: java.vo.List<java.time.ZonedDateTime>
     **/
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityMjZoneDate> selectZonedDateTimeList(Long companyId) {
        QueryWrapper<EntityMjZoneDate> wrapper = new QueryWrapper<>();
        wrapper.select("day_date","date","zone_no");
        wrapper.eq("com_id",companyId);
        List<EntityMjZoneDate> list=daoZoneDate.selectList(wrapper);
        return list;
    }

    /*
     * @Description:获取时区设置列表,分页
     * @Author: lu
     * @Date: 2022/1/13 16:28
     * @param getZoneDateListRequest:
     * @return: java.vo.List<com.ocom.common.entity.access.EntityMjZoneDate>
     **/
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityMjZoneDate> selectZonedDateTimeListPage(GetZoneDateListRequest getZoneDateListRequest,Long companyId) {
        PageHelper.startPage(getZoneDateListRequest.getPage(), getZoneDateListRequest.getSize());

        QueryWrapper<EntityMjZoneDate> wrapper = new QueryWrapper<>();
        wrapper.select("day_date","date","zone_no");
        wrapper.eq("com_id",getZoneDateListRequest.getComId());
        List<EntityMjZoneDate> list=daoZoneDate.selectList(wrapper);
        PageInfo pageInfo = new PageInfo<>(list);
        return pageInfo.getList();
    }

    /*
     * @Description:设置日期时区（setDateZone）
     * @Author: lu 
     * @Date: 2022/1/14 11:16
     * @param entityMjZoneDate:  
     * @return: com.ocom.common.resp.Result
     **/
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Transactional
    @Override
    public Integer setDateZone(EntityMjZoneDate entityMjZoneDate,Long companyId) {
		QueryWrapper<EntityMjZoneDate> wrapper = new QueryWrapper<>();
        wrapper.select("id");
        wrapper.eq("com_id",entityMjZoneDate.getComId());
        wrapper.eq("day_date",entityMjZoneDate.getDayDate());
        wrapper.eq("date",entityMjZoneDate.getDate());
        List<EntityMjZoneDate> list=daoZoneDate.selectList(wrapper);
        //判断查询的数据是否为空。为空就新增入库，不为空就修改zone_no字段。
        if (list.size()>0){
            for (EntityMjZoneDate mjZoneDate : list) {
                EntityMjZoneDate entityMjZoneDates=new EntityMjZoneDate();
                entityMjZoneDates.setId(mjZoneDate.getId());
                entityMjZoneDates.setZoneNo(entityMjZoneDate.getZoneNo());
                entityMjZoneDates.setUpdateTime(new Date());
                int i=daoZoneDate.updateById(entityMjZoneDates);
                return i;
            }

            return 1;
        }else {
            entityMjZoneDate.setCreateTime(new Date());
            entityMjZoneDate.setUpdateTime(new Date());
            int i=daoZoneDate.insert(entityMjZoneDate);
            return 1;
        }
    }

    /*
     * @Description: 删除日期时区
     * @Author: lu 
     * @Date: 2022/1/14 16:32
     * @param entityMjZoneDate: 
     * @param comId:  
     * @return: com.ocom.common.resp.Result
     **/
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Transactional
    @Override
    public Integer removeDateZone(EntityMjZoneDate entityMjZoneDate, Long companyId) {
        QueryWrapper<EntityMjZoneDate> wrapper = new QueryWrapper<>();
        wrapper.select("id");
        wrapper.eq("com_id",entityMjZoneDate.getComId());
        wrapper.eq("day_date",1);
        wrapper.eq("date",entityMjZoneDate.getDate());
        List<EntityMjZoneDate> list=daoZoneDate.selectList(wrapper);
        if (list.size()>0){
            for (EntityMjZoneDate mjZoneDate : list) {
                daoZoneDate.deleteById(mjZoneDate.getId());
            }
            return 1;
        }else {
            return  null;
        }
    }

    //从门禁时区设置表mj_zone_date查找com_id= companyID、date_day= dateDay、date= accessDay的记录,
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityMjZoneDate getZoneDateInfo(Long companyId,Integer dateDay, String weekDay) {
        QueryWrapper<EntityMjZoneDate> query = Wrappers.query();
        query.eq("com_id",companyId);
        query.eq("day_date",dateDay);
        query.eq("date",weekDay);
        query.last("limit 1");
        return daoZoneDate.selectOne(query);
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public Long getZoneNo(Long companyId) {
          Calendar cal = Calendar.getInstance(); // 获得一个日历
          cal.setTime(new Date());
          Integer weekDayInt = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
          if (weekDayInt < 0)
              weekDayInt = 0;

          String weekDay  = weekDayInt.toString();
          //时区初始化 zoneNo = 0
          Long zoneNo = 0L;
          //先从门禁时区设置表mj_zone_date查找com_id= companyID、date_day= 0、date= accessDay的记录,
          Integer dateDay= 0;
          EntityMjZoneDate zoneDateInfo = getZoneDateInfo(companyId,dateDay, weekDay.toString());
          //	如果有记录：zoneNo = mj_zone_date . zone_no
          if (zoneDateInfo != null){
              zoneNo = zoneDateInfo.getZoneNo();
          }

          //再从门禁时区设置表mj_zone_date查找com_id= companyID、date_day= 1、date= accessDate的记录,
          dateDay=1;
          weekDay = DateUtils.getDate();
          EntityMjZoneDate dateInfo = getZoneDateInfo(companyId, dateDay, weekDay);
          //	如果有记录：zoneNo = mj_zone_date . zone_no
          if (dateInfo != null){
              zoneNo = dateInfo.getZoneNo();
          }
          //		如果 zoneNo == 0：返回2；
          return zoneNo;
    }



}
